import { AxesHelper, PerspectiveCamera, Scene, WebGLRenderer, PointsMaterial } from "three"
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { PCDLoader } from "three/examples/jsm/loaders/PCDLoader";

export default class ContentManager {
    private scene: Scene;
    private controls: OrbitControls;
    private camera: PerspectiveCamera;
    private renderer: WebGLRenderer;

    constructor(content3d: HTMLElement) {
        const width = content3d.clientWidth;
        const height = content3d.clientHeight;
        this.renderer = new WebGLRenderer();
        const threeContainer = this.renderer.domElement;
        this.renderer.setSize(width, height);
        content3d.appendChild(threeContainer);

        this.scene = new Scene();
        this.camera = new PerspectiveCamera(75, width / height, 1, 1000);
        this.camera.position.set(-0.01, 0, 50);
        this.camera.up.set(0, 0, 1);
        this.camera.lookAt(0, 0, 0);
        this.controls = new OrbitControls(
            this.camera,
            threeContainer
        );
        const axes = new AxesHelper(2);
        this.scene.add(axes);
        this.render();
        this.init();
    }

    private init(): void {
        const pcdLoader = new PCDLoader();
        pcdLoader.load(
            "/data/test.pcd",
            (pcd: any) => {
                pcd.scale.set(1, 1, 1);

                // pcd.rotation.y+=0.5*Math.PI;

                pcd.material = new PointsMaterial({
                    color: 0xff0000,
                    size: 0.005 //点对象像素尺寸
                });
                
                this.scene.add(pcd);
            }
        );
    }

    private render(): void {
        requestAnimationFrame(this.render.bind(this));
        (this.controls as OrbitControls).update();
        this.renderer.render(this.scene, this.camera);
    }
}